package com.example.demo.controller;

import com.example.demo.entity.TestPaper;
import com.example.demo.service.PaperService;
import com.example.demo.utils.R;
import com.example.demo.utils.ReSet;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@RestController
public class PaperController {
    @Resource
    private PaperService paperService;

    /**
     * 查询章节试卷
     * */
    @PostMapping(value = "selpaper")
    public R selpaper(Integer chapter_id) {
        TestPaper list = paperService.selpaper(chapter_id);
        if (list != null){
            return ReSet.Ok(list);
        }
        return ReSet.Err("查询失败，数据不存在");
    }

    /**
     *查询所有试卷
     *  */
    @PostMapping(value = "selallpaper")
    public R selallpaper() {
        List<TestPaper> list = paperService.selallpaper();
        if (list != null){
            return ReSet.Ok(list);
        }
        return ReSet.Err("查询失败，数据不存在");
    }

    /**
     * 增加试卷
     * */
    @PostMapping(value = "inspaper")
    public R inspaper(String test_paper_name,Integer chapter_id) {
        int i = paperService.inspaper(test_paper_name,chapter_id);
        if (i > 0){
            return ReSet.Ok(i);
        }
        return ReSet.Err("增加试卷操作失败");
    }

    /**
     * 更改试卷
     * */
    @PostMapping(value = "updpaper")
    public R updpaper(String test_paper_name, Integer chapter_id, Integer test_paper_id) {
        int i = paperService.updpaper(test_paper_name,chapter_id,test_paper_id);
        if (i > 0){
            return ReSet.Ok(i);
        }
        return ReSet.Err("更改试卷操作失败");
    }

    /**
     * 删除试卷
     * */
    @PostMapping(value = "delpaper")
    public R delpaper(Integer test_paper_id) {
        int i =  paperService.delpaper(test_paper_id);
        if (i > 0){
            return ReSet.Ok(i);
        }
        return ReSet.Err("删除试卷操作失败");
    }

    /**
     * 改变试卷状态
     * */
    @PostMapping("updpastu")
    public R updpastu(Integer test_paper_id) {
        int i = paperService.updpastu(test_paper_id);
        if (i > 0){
            return ReSet.Ok(i);
        }
        return ReSet.Err();
    }
    /**
     *已做试卷试题记录
     * */
    @PostMapping("selrecordtq")
    public R selrecordtq(Integer tpid,Integer uid){

        List<Integer> list = paperService.seltqrecord(tpid, uid);//查询已做试卷试题ID
        return ReSet.Ok(list);
    }

    @PostMapping("selrecordtq1")
    public R selrecordtq1(Integer tpid,Integer uid){

        List<Integer> list = paperService.seltqrecord(tpid, uid);//查询已做试卷试题ID
        List<Integer> list2 = paperService.seltqrecordopid(tpid, uid);//查询已做试卷试题选项ID
        ArrayList arrayList = new ArrayList();
        int i = list.size();
        for (int j = 0;j < i;j++){
            int k = (int)list.get(j);
//            int m = (int)list2.get(j);
            ArrayList arrayList1 = new ArrayList();
            List names = paperService.selrecordtqname(k);
            List list1 = paperService.selrtqopt(k);

            arrayList1.add(names);
            arrayList1.add(list2.get(j));
            arrayList1.add(list1);
            arrayList.add(arrayList1);

        }
        return ReSet.Ok(arrayList);
    }
}
